public class Solution {
    public int mySqrt(int x) {
        if (x == 1) {
            return 1;
        }

        int low = x;
        int high = x;

        if (x > 1) {
            low = 1;
        } else {
            high = 1;
        }

        while (low < high) {
            int middle = (low + high) / 2;
            int val = x / middle;
            if (val == middle) {
                return middle;
            } else if (val > middle) {
                low = middle;
            } else {
                high = middle;
            }
        }
        return low;
    }
}